﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jungle.Code.DAL;
using Jungle.Code.Utils;

public partial class tree : System.Web.UI.UserControl
{
    protected override void Render(HtmlTextWriter writer)
    {
        writer.AddAttribute(HtmlTextWriterAttribute.Width, "220px");
        writer.AddAttribute(HtmlTextWriterAttribute.Bgcolor, "#FFFFFF");
        writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "15");
        writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, "solid");
        writer.AddStyleAttribute(HtmlTextWriterStyle.BorderColor, "#FFFFFF");
        writer.RenderBeginTag(HtmlTextWriterTag.Table);
        writer.RenderBeginTag(HtmlTextWriterTag.Tr);
        writer.RenderBeginTag(HtmlTextWriterTag.Td);
        writer.AddAttribute(HtmlTextWriterAttribute.Href, "Modules.aspx?mod=Articles&cmd=addcategory");
        writer.RenderBeginTag(HtmlTextWriterTag.A);
        writer.AddAttribute(HtmlTextWriterAttribute.Src, "../Modules/News/img/addCategory.jpg");
        writer.AddAttribute(HtmlTextWriterAttribute.Alt, "Добавить категорию");
        writer.RenderBeginTag(HtmlTextWriterTag.Img);
        writer.RenderEndTag();
        writer.RenderEndTag();
        writer.Write("&nbsp;<strong>Категории:</strong>");
        writer.Write("<br>");

        using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
        {
            SqlCommand command = new SqlCommand("articles.mdNews_GetAll", connect);
            SqlParameter param_type = command.Parameters.Add("@type", SqlDbType.NChar, 20);
            SqlDataReader reader = null;
            command.CommandType = CommandType.StoredProcedure;
            param_type.Value = "GetCategories";

            command.Connection.Open();
            reader = command.ExecuteReader();
            while (reader.Read())
            {
                writer.Write("&nbsp;&nbsp;&nbsp;");
                writer.AddAttribute(HtmlTextWriterAttribute.Href, "Modules.aspx?mod=Articles&cmd=nbc&id_category=" + reader.GetValue(0).ToString());
                writer.RenderBeginTag(HtmlTextWriterTag.A);
                writer.AddAttribute(HtmlTextWriterAttribute.Src, "../Modules/Articles/img/category.jpg");
                writer.RenderBeginTag(HtmlTextWriterTag.Img);
                writer.RenderEndTag();
                if ((Request["id_category"] != null) && (Request["id_category"].ToString() == reader.GetValue(0).ToString()))
                {
                    writer.Write("&nbsp;<strong>" + reader.GetValue(1) + "</strong>");
                }
                else
                writer.Write("&nbsp;" + reader.GetValue(1));
                writer.RenderEndTag();

                writer.AddAttribute(HtmlTextWriterAttribute.Href, "Modules.aspx?mod=Articles&cmd=deleteC&id_category=" + reader.GetValue(0).ToString());
                writer.RenderBeginTag(HtmlTextWriterTag.A);
                writer.AddAttribute(HtmlTextWriterAttribute.Src, "../Modules/Articles/img/del2.jpg");
                writer.AddAttribute(HtmlTextWriterAttribute.Alt, "Удалить");
                writer.RenderBeginTag(HtmlTextWriterTag.Img);
                writer.RenderEndTag();
                writer.RenderEndTag();
                writer.Write("<br>");

            }
            reader.Close();
            command.Connection.Close();
        }

        writer.Write("<br>");
        writer.AddAttribute(HtmlTextWriterAttribute.Href, "Modules.aspx?mod=Articles&cmd=News");
        writer.RenderBeginTag(HtmlTextWriterTag.A);
        writer.AddAttribute(HtmlTextWriterAttribute.Alt, "Полный список статей");
        writer.AddAttribute(HtmlTextWriterAttribute.Src, "../Modules/Articles/img/rss.jpg");
        writer.RenderBeginTag(HtmlTextWriterTag.Img);
        writer.RenderEndTag();
        writer.Write("&nbsp;<strong>Все статьи</strong>");
        writer.RenderEndTag();

        writer.RenderEndTag();
        writer.RenderEndTag();
        writer.RenderEndTag();
     
        base.Render(writer);
    }

}
